package com.example.demo.verify.validator;

import com.example.demo.verify.ArrayValid;
import com.example.demo.common.Valid;
import com.example.demo.common.constant.RuleCode;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class ArrayValidValidator implements ConstraintValidator<ArrayValid, Object> {
    private String name;
    private int rule;
    private boolean empty;

    @Override
    public void initialize(ArrayValid param) {
        this.name = param.name();
        this.rule = param.rule();
        this.empty = param.empty();
    }

    @Override
    public boolean isValid(Object obj, ConstraintValidatorContext context) {
        // 设置校验出错提示
        context.disableDefaultConstraintViolation();

        // 数组参数不能为空
        if (obj == null || obj.toString().trim().isEmpty() || obj.toString().equals("[]")) {
            context.buildConstraintViolationWithTemplate(this.name + "不能为空").addConstraintViolation();
            return false;
        }

        // 数组对象转换
        String[] list;
        try {
            list = obj.toString().replaceAll("\\[", "").replaceAll("]", "").split(",");
        }
        catch (Exception ex) {
            context.buildConstraintViolationWithTemplate(this.name + "格式异常").addConstraintViolation();
            return false;
        }

        // 校验所有数组元素
        boolean isReturn = false;
        int length = list.length;
        for (int i = 0; i < length; i++) {
            if (this.empty) {
                if (list[i] == null) {
                    isReturn = true;
                }
                else if (list[i].trim().isEmpty()) {
                    continue;
                }
            }
            else {
                // 不能为null或为空
                if (list[i] == null || list[i].trim().isEmpty()) {
                    isReturn = true;
                }
            }
            if (this.rule == RuleCode.noEmpty) {
                continue;
            }
            if (isReturn) {
                context.buildConstraintViolationWithTemplate(this.name + "的第" + (i + 1) + "个元素为空").addConstraintViolation();
                return false;
            }

            // 根据规则类型进行校验
            String value = list[i].trim();
            if (this.rule == RuleCode.noWick) {
                isReturn = Valid.isWicked(value);
            }
            else if (this.rule == RuleCode.phone) {
                isReturn = !Valid.isPhone(value);
            }
            else if (this.rule == RuleCode.email) {
                isReturn = !Valid.isEmail(value);
            }
            else if (this.rule == RuleCode.mobile) {
                isReturn = !Valid.isMobile(value);
            }
            else if (this.rule == RuleCode.contact) {
                isReturn = !Valid.isContact(value);
            }
            else if (this.rule == RuleCode.num) {
                isReturn = !Valid.isNum(value);
            }
            else if (this.rule == RuleCode.money) {
                isReturn = !Valid.isMoney(value);
            }
            else if (this.rule == RuleCode.date) {
                isReturn = !Valid.isDate(value);
            }
            else if (this.rule == RuleCode.base64) {
                isReturn = !Valid.isBase64(value);
            }
            else if (this.rule == RuleCode.url) {
                isReturn = !Valid.isUrl(value);
            }
            else {
                isReturn = true;
            }

            if (isReturn) {
                context.buildConstraintViolationWithTemplate(this.name + "的第" + (i + 1) + "个元素格式错误").addConstraintViolation();
                return false;
            }
        }

        return true;
    }
}
